#!/bin/bash
# Search for a BlueSwoop adapter and pair with it

# This is the channel number for the BlueTooth adapter (default ch. 1 for RN-42)
channel=1

# Un-bind any rfcomm adapters which are already bound
if [ -c "/dev/rfcomm0" ]; then
    echo -n "Releasing existing /dev/rfcomm0 device..."
    sudo rfcomm release 0;
    echo "done."
fi

echo -n "Scanning for BlueSwoop adapter..."
the_adapter=`hcitool scan | egrep -i "BlueSwoop|RN42" | egrep -o "([0-9A-F][0-9A-F]:){5}[0-9A-F][0-9A-F]"`
if [ "$the_adapter" == "" ]; then
    echo "No adapter found!";
else
    echo "found adapter with address $the_adapter." 
    sudo rfcomm bind 0 $the_adapter $channel 
    sudo chmod 0666 /dev/rfcomm0
fi

